//
//  ViewController.m
//  Commet
//
//  Created by tanqilong on 16/5/31.
//  Copyright © 2016年 tanqilong. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "CMCometNetWorking.h"
#import "TestDao.h"

@interface ViewController ()
@property (nonatomic,strong) NSURLSessionDataTask * dataTask;
@property (nonatomic,strong) CMCometNetWorking * comet;
@property (nonatomic,strong) TestDao * testDao;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

//默认配置comet
- (IBAction)cometButtonClicked:(id)sender {
    _comet = [CMCometNetWorking CometNetByUrl:@"http://172.32.1.38:8089/hqapi/asyn/ticker" paramDic:@{@"symbol":@"AG.NJS"} complete:^(id result) {
        NSLog(@"%@",result);
    } Error:^(NSError * error) {
        NSLog(@"%@",error);
    }];
    [_comet resume];
}

//自定义配置comet
- (IBAction)selfComet:(id)sender {
    NSURLSessionConfiguration * config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    config.timeoutIntervalForRequest = 5;
    _comet = [CMCometNetWorking CometNetByUrl:@"http://172.32.1.38:8089/hqapi/asyn/ticker" paramDic:@{@"symbol":@"AG.NJS"} sessionConfiguration:config complete:^(id result) {
        NSLog(@"%@",result);
    } Error:^(id error) {
        NSLog(@"%@",error);
    }];
    [_comet resume];
}

//制空comet
- (IBAction)nilComet:(id)sender {
    _comet = nil;
}

//开始访问
- (IBAction)startComet:(id)sender {
    [_comet resume];
}
//暂停
- (IBAction)suspend:(id)sender {
    [_comet suspend];
}
- (IBAction)cancelButtonClicked:(id)sender {
    [_comet cancel];
}
//DAO层测试
- (IBAction)daoTest:(id)sender {
//    [TestDao requestWithParameters:(NSMutableDictionary *)@{@"symbol":@"AG.NJS"} IndicatorView:self.view LoadingFinished:^(id resulte) {
//        NSLog(@"%@",resulte);
//    } CacelLoading:nil LoadingError:^(id resulte) {
//        
//    }];
    
    [TestDao requestWithParameters:nil IndicatorView:self.view LoadingFinished:^(id resulte) {
        NSLog(@"%@",resulte);
    } CacelLoading:nil LoadingError:^(id resulte) {
        
    }];
}
- (IBAction)daoComet:(id)sender {
    _testDao = [TestDao cometWithParameters:(NSMutableDictionary *)@{@"symbol":@"AG.NJS"} LoadingFinished:^(id resulte) {
        NSLog(@"%@",resulte);
    } LoadingError:^(id resulte) {
        
    }];
}
//dao comet暂停
- (IBAction)daoCometSusspend:(id)sender {
    [_testDao suspend];
}
- (IBAction)daoResume:(id)sender {
    [_testDao resume];
}
//dao释放
- (IBAction)daoRelease:(id)sender {
    _testDao = nil;
}



-(void)startComet {
    NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager * manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:config];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSURL * url = [NSURL URLWithString:@"http://172.32.1.38:8089/hqapi/asyn/ticker?symbol=AG.NJS"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    NSURLSessionDataTask * dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            NSLog(@"Error");
        } else {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@", dic);
        }
    }];
    self.dataTask = dataTask;
    [dataTask resume];
    
    [self performSelector:@selector(test) withObject:nil afterDelay:3];
}

-(void)test {
    NSLog(@"%@",self.dataTask);
    [self.dataTask resume];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
